package ui.panels;

import java.awt.Font;
import java.util.Calendar;
import java.util.Date;
import javax.swing.ListSelectionModel;
import manager.GenericDriver;
import model.ViewTimeSlotTableModel;
import ui.windows.RemedySchedulerFrame;

public class TimePanel extends javax.swing.JPanel {

    /** Creates new form TimePanel */
    public TimePanel(RemedySchedulerFrame mainFrame) {
        initComponents();
        this.mainFrame = mainFrame;
        includeModel();
    }

    private void includeModel() {
        new Thread() {

            @Override
            public void run() {
                startTime = Calendar.getInstance().getTime();
                while (true) {
                    Date currentTime = Calendar.getInstance().getTime();
                    lbTime.setText(currentTime.toString());
                    int compareTime = (currentTime.getHours() - startTime.getHours()) * 3600
                            + (currentTime.getMinutes() - startTime.getMinutes()) * 60
                            + (currentTime.getSeconds() - startTime.getSeconds());
                    int hour = compareTime / 3600;
                    int minute = (compareTime - hour) / 60;
                    int second = compareTime % 60;
                    lbTimeC.setText(hour + ":" + minute + ":" + second);
                    try {
                        sleep(1000);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }.start();
        
        driver = mainFrame.getGenericDriver();
        model = new ViewTimeSlotTableModel(driver.showShift());
        timeSlotTable.setModel(model);
        timeSlotTable.getTableHeader().setReorderingAllowed(false);
        timeSlotTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        timeSlotTable.setFillsViewportHeight(true);
        timeSlotTable.setEnabled(false);
        timeSlotTable.getTableHeader().setFont(new Font(null, Font.BOLD, 11));
        timeSlotTable.getColumnModel().getColumn(0).setPreferredWidth(20);
        timeSlotTable.getColumnModel().getColumn(1).setPreferredWidth(70);
        timeSlotTable.getColumnModel().getColumn(2).setPreferredWidth(55);
        timeSlotTable.getColumnModel().getColumn(3).setPreferredWidth(55);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private GenericDriver driver;
    private ViewTimeSlotTableModel model;
    private Date startTime;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCalendar1 = new com.toedter.calendar.JCalendar();
        lbTime = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lbTimeC = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        timeSlotTable = new javax.swing.JTable();

        setBackground(new java.awt.Color(0, 51, 51));

        jCalendar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbTime.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbTime.setForeground(new java.awt.Color(204, 255, 204));
        lbTime.setText("Time");

        jLabel1.setForeground(new java.awt.Color(204, 255, 204));
        jLabel1.setText("Time Counter:");

        lbTimeC.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbTimeC.setForeground(new java.awt.Color(204, 255, 204));
        lbTimeC.setText("value");

        timeSlotTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(timeSlotTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(lbTime, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(lbTimeC))
                        .addComponent(jCalendar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbTime)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCalendar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 101, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbTimeC))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JCalendar jCalendar1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbTime;
    private javax.swing.JLabel lbTimeC;
    private javax.swing.JTable timeSlotTable;
    // End of variables declaration//GEN-END:variables
}
